﻿using System;
using System.Linq;
using System.Web.Mvc;
using Rob.SampleCode.MyApp.Core;
using Rob.SampleCode.MyApp.Core.Domain;

namespace Rob.SampleCode.MyApp.MVC.Controllers
{
    public class HomeController : Controller
    {
        public HomeController(IRepository repository)
        {
            _repository = repository;
        }

        private readonly IRepository _repository;

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult GenerateError()
        {
            throw new NotImplementedException("testing error handling");
        }

        public ActionResult Test()
        {
            var customers = _repository
                .Find<Customer>()
                .Take(10)
                .ToArray()
                .Select(x => new
                    {
                        x.CustomerID,
                        x.CompanyName,
                        x.ContactName,
                        x.ContactTitle,
                        x.Address,
                        x.City,
                        x.Region,
                        x.PostalCode,
                        x.Country,
                        x.Phone,
                        x.Fax,
                        OrderCount = x.Orders.Count(),
                        DemographicCount = x.Demographics.Count(),
                    });

            return Json(customers, JsonRequestBehavior.AllowGet);
        }
    }
}